/*
 * @Author: ✨Fireflx 
 * @Date: 2022-02-16 21:39:40 
 * @Last Modified by: ✨Fireflx
 * @Last Modified time: 2022-03-06 13:16:26
 */

const path = require("path")
const fs = require('fs')
const NodeRSA = require('node-rsa')
// const targetUrl = path.resolve(__dirname)

class KeyControl {//🌟//🌟//🌟
    static createKey = async () => {
        const key = new NodeRSA({ b: 512 });
        const publicKey = key.exportKey('public');
        const privateKey = key.exportKey('private');

        await fs.promises.writeFile(__dirname + "/publicKey.key",
            publicKey, {
            encoding: 'utf-8',
        });
        await fs.promises.writeFile(__dirname + '/privateKey.key',
            privateKey, {
            encoding: 'utf-8'
        })
    }
    constructor() {
        KeyControl.createKey()
        this.getKey = async () => {
            return await fs.promises.readFile(__dirname + "/publicKey.key", {
                encoding: 'utf-8'
            })
        }
        this.decrypt = async str => {
            const key = await fs.promises.readFile(__dirname+"/privateKey.key",{
                encoding:'utf-8',
            })
            return new NodeRSA(key).decrypt(str,'utf-8')//🌟
        }
    }

}

module.exports = new KeyControl()
